// pages/order/order-details/order-details.js
var httpUtil = require('../../../../utils/httpUtil.js');
var uiUtil = require('../../../../utils/uiUtil.js');
const app = getApp();
var that, timeInterval;
var time=0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderDetails:null, //订单详情,
    payStatus: "0", //0,默认不显示；1，支付超时；2，正常显示
    payTime: ["00", "00", "00"] //支付剩余时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    that.setData({
      pages: app.globalData.pages,
      isH5: options.isH5||false,
      orderNoIndex:options.index||""
    });
    that.getOrderDetails(options.orderNo);
  },

  /**
   * 订单详情查询
   */
  getOrderDetails(orderNo){
    httpUtil.PostRequest("searchUserOrderDetail", {
      orderNo: orderNo
    }, {
        success(res) {
          console.log(res)
          if(that.data.isH5){
            if(res.orderStatus==0){
              res.orderStatus=-1;
            }
          }
          if (res.orderStatus==0){
            that.surplusPayTime(res.orderEffectTime); //开启支付倒计时
          }
          that.setData({
            orderDetails:res
          })
          if(that.data.isH5){
            if(res.orderStatus==-1){
              if(time<60000){
                setTimeout(function(){
                  time+=2000;
                  console.log(time);
                  that.getOrderDetails(orderNo);
                },time)
              } else {
                res.orderStatus=9;
                that.setData({
                  orderDetails:res
                })
              }
            }
          }
        },
        fail(code, res) {
          uiUtil.showToast(res);
        },
        complete() { }
      })
  },

  // 支付剩余时间
  surplusPayTime(res) {
    var maxtime = Math.floor(parseInt(res) / 1000);
    var day, hour, minutes, seconds, d, h, m, s, time, payTime;
    timeInterval = setInterval(() => {
      if (maxtime >= 0) {
        if (maxtime > 86400) { //倒计时大于一天
          day = Math.floor(maxtime / 86400);
          hour = Math.floor((maxtime % 86400) / 3600);
          minutes = Math.floor(((maxtime % 86400) % 3600) / 60);
          if (hour < 10) { h = "0"; } else { h = ""; }
          if (minutes < 10) { m = "0"; } else { m = ""; }
          time = String(day) + " 天" + "," + h + String(hour) + "," + m + String(minutes);
        } else { //倒计时小于一天
          hour = Math.floor(maxtime / 3600);
          minutes = Math.floor((maxtime % 3600) / 60);
          seconds = Math.floor((maxtime % 3600) % 60);
          if (hour < 10) { h = "0"; } else { h = ""; }
          if (minutes < 10) { m = "0"; } else { m = ""; }
          if (seconds < 10) { s = "0"; } else { s = ""; }
          time = h + String(hour) + "," + m + String(minutes) + "," + s + String(seconds);
        }

        payTime = time.split(",");
        that.setData({
          payTime: payTime
        });
        --maxtime;

      } else { //支付时间结束
        uiUtil.showToast("支付超时");
        // that.setData({
        //   payStatus:"1"
        // })
        that.getOrderDetails(that.data.orderDetails.orderNo);
        clearInterval(timeInterval);
      }
    }, 1000);
  },


  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {
    clearInterval(timeInterval); //页面卸载，清除支付倒计时
    // if(that.data.isH5){
    //   wx.reLaunch({
    //     url: "/pages/index/navigationbar/navigationbar?iconPageThat=order"
    //   })
    // }
  },


  /**
   * 取消订单
   */
  delOrder(){
    let orderNo = that.data.orderDetails.orderNo;
    httpUtil.PostRequest("delOrder", {
      orderNo: orderNo
    }, {
        success(res) {
          uiUtil.showToast("取消成功");
          that.getOrderDetails(orderNo);

          var pages = getCurrentPages();
          var lastPage = pages[0];
          if (lastPage.route == "pages/index/navigationbar/navigationbar") {
            let orderNo=lastPage.data.orderLists.dataList[that.data.orderNoIndex].orderNo;
            if(orderNo==that.data.orderDetails.orderNo){
              let orderStatus="orderStatus.orderStatus["+that.data.orderNoIndex+"].orderStatus";
              lastPage.setData({
                [orderStatus]: "8"
              })
            } else {
              lastPage.refresherrefreshOrder();
            }
            
          }
        },
        fail(code, res) {
          uiUtil.showToast(res);
        },
        complete() { }
      })
  },

  /**
   * 确认送达
   */
  orderStatusChange(e){
    let orderNo = that.data.orderDetails.orderNo;
    let status = e.currentTarget.dataset.status;

    httpUtil.PostRequest("updateUserOrderStatus", {
      orderNo: orderNo,
	    status:status
    }, {
        success(res) {
          uiUtil.showToast("确认成功");
          that.getOrderDetails(orderNo);

          var pages = getCurrentPages();
          var lastPage = pages[0];
          if (lastPage.route == "pages/index/navigationbar/navigationbar") {
            let orderNo=lastPage.data.orderLists.dataList[that.data.orderNoIndex].orderNo;
            if(orderNo==that.data.orderDetails.orderNo){
              let orderStatus="orderLists.dataList["+that.data.orderNoIndex+"].orderStatus";
              lastPage.setData({
                [orderStatus]: status
              })
            } else {
              lastPage.refresherrefreshOrder();
            }
          }
        },
        fail(code, res) {
          uiUtil.showToast(res);
        },
        complete() { }
      })
  }
})